import {reactive} from "vue";
import dictHelp from "@/utils/dicthelp.js";

let changed = []

export default {
    layout:
        [
            {
                formType: 'grid-tailwind',
                colNumber: 2,
                cols: [
                    {
                        formList: [
                            {dataIndex: "name"},
                            {dataIndex: "type"},
                            {dataIndex: "parent_id"},
                            {dataIndex: "service_group"},
                            {dataIndex: "first_leg_id"},
                            {dataIndex: "departure"},
                            {dataIndex: "destination"},
                            {dataIndex: "supplier_service_id"},
                            {dataIndex: "remark"},
                        ]
                    },
                    {
                        formList: [
                            {dataIndex: "company"},
                            {dataIndex: "channel"},
                            {dataIndex: "voyage"},
                            {dataIndex: "path"},
                            {dataIndex: "expecte_clear_at"},
                            {dataIndex: "expecte_eta"},
                            {dataIndex: "expecte_departure_at"},
                            {dataIndex: "close_at"},
                        ]
                    }
                ]
            }
        ],
    columns: reactive([
        {
            title: "主键",
            dataIndex: "id",
            formType: "input",
            addDisplay: false,
            editDisplay: false,
            hide: true,
            commonRules: {
                required: true,
                message: "请输入主键"
            }
        },
        {
            title: "名称",
            dataIndex: "name",
            formType: "input",
            commonRules: {
                required: true,
                message: "请输入名称"
            }
        },
        {
            title: "分组",
            dataIndex: "parent_id",
            formType: "select",
            search: true,
            dict: {
                url: 'logistics/shippingScheduleGroup/list',
                props: {label: 'name', value: 'id'},
                translation: true,
            },
            commonRules: {
                required: true,
                message: "请选择分组"
            }
        },
        {
            title: "类型",
            dataIndex: "type",
            formType: "radio",
            type: 'button',
            search: true,
            onChange: (value, maFormObject) => {
                let form = maFormObject.formModel.value
                delete form.departure
                delete form.destination
                delete form.first_leg_id
                delete form.supplier_service_id
                // 类型切换
                let dict = maFormObject.getColumnService().dicts
                let columnService = maFormObject.getColumnService()
                columnService.get('departure').setAttr('display', true)
                columnService.get('destination').setAttr('display', true)
                columnService.get('first_leg_id').setAttr('display', true)
                columnService.get('supplier_service_id').setAttr('display', true)
                // 切换船期类型
                columnService.getDictService('departure').loadDict(dictHelp.place_with_type(value), dict)
                columnService.getDictService('destination').loadDict(dictHelp.place_with_type(value), dict)
                columnService.getDictService('supplier_service_id').loadDict(dictHelp.supplier_service_with_type(value), dict)
                maFormObject.getColumnService().columns.map(i => {
                    if(i.dataIndex == 'parent_id'){
                        i.rules = value == 1 ? {required: true, message: '请选择分组'} : {}
                    }
                })
            },
            commonRules: {
                required: true,
                message: "请输入类型"
            },
            dict: {
                name: "shipping_schedule_type",
                props: {
                    label: "title",
                    value: "key"
                },
                translation: true
            }
        },
        {
            title: "出发地",
            dataIndex: "departure",
            formType: "select",
            addDisplay: false,
            search: true,
            dict: dictHelp.place,
            commonRules: {
                required: true,
                message: "请输入出发地"
            }
        },
        {
            title: "目的地",
            dataIndex: "destination",
            formType: "select",
            addDisplay: false,
            search: true,
            dict: dictHelp.place,
            commonRules: {
                required: true,
                message: "请输入目的地"
            }
        },
        {
            title: "服务分组",
            dataIndex: "service_group",
            formType: "tree-select",
            multiple:true,
            hide:true,
            virtualListProps:{},
            commonRules: {
                required: true,
                message: "请选择服务"
            },
            dict: {
              url: '/logistics/serviceGroup/tree',
            //   props: {label: 'name', value: 'id'},
            //   params: {select: 'id,name'},
              translation: true,
            },
            editDefaultValue: (record) => {
                return record.service_group == 0 ? undefined : record.service_group
              },
            onChange:(val, maFormObject)=>{
                let dict = maFormObject.getColumnService().dicts
                dict['service_group'].map(i=>{
                    if(val.includes(i.value)){
                        i.children.map(j=>{
                            if(!val.includes(j.value) && !changed.includes(i.value)){
                                val.push(j.value)
                            }
                        })
                    }else{
                        if(changed.includes(i.value)){
                            i.children.map(j=>{
                                let index = val.indexOf(j.value);
                                if (index > -1) {
                                    val.splice(index, 1);
                                }
                            }) 
                        }
                    }
                })
                maFormObject.formModel.value.service_group = val
                changed = val
            }  
          },
        {
            title: "头程",
            dataIndex: "first_leg_id",
            formType: "select",
            addDisplay: false,
            search: true,
            dict: dictHelp.firstLegTransportation,
            onChange: (value, maFormObject) => {
                let form = maFormObject.formModel.value
                let dict = maFormObject.getColumnService().dicts['first_leg_id'].find(i => i.value == value)
                if (dict) {
                    form.departure = dict.data.departure
                    form.destination = dict.data.destination
                }
            },
        },
        {
            title: "供应商服务",
            dataIndex: "supplier_service_id",
            addDisplay: false,
            dict: dictHelp.supplier_service,
            virtualListProps:{},
            formType: "select",
            editDefaultValue: (record) => {
                return [undefined,'null',null].includes(record.supplier_service_id) ? undefined : record.supplier_service_id
              },
        },
        {
            title: "公司",
            dataIndex: "company",
            formType: "input"
        },
        {
            title: "渠道",
            dataIndex: "channel",
            formType: "input"
        },
        {
            title: "航次",
            dataIndex: "voyage",
            formType: "input"
        },
        {
            title: "路径",
            dataIndex: "path",
            formType: "input"
        },
        {
            title: "预计清关时间",
            dataIndex: "expecte_clear_at",
            formType: "date",
            hide: true,
            // showTime: false
        },
        {
            title: "预计到达时间",
            dataIndex: "expecte_eta",
            hide: true,
            formType: "date",
            // showTime: false
        },
        {
            title: "预计出发时间",
            dataIndex: "expecte_departure_at",
            hide: true,
            formType: "date",
            // showTime: true
        },
        {
            title: "截单时间",
            dataIndex: "close_at",
            hide: true,
            formType: "date",
            commonRules: {
                required: true,
                message: "请选择截单时间"
            },
            showTime: true
        },
        {
            title: "创建者",
            dataIndex: "created_by",
            formType: "input",
            addDisplay: false,
            editDisplay: false,
            hide: true
        },
        {
            title: "更新者",
            dataIndex: "updated_by",
            formType: "input",
            addDisplay: false,
            editDisplay: false,
            hide: true
        },
        {
            title: "创建时间",
            dataIndex: "created_at",
            formType: "date",
            addDisplay: false,
            editDisplay: false,
            hide: true,
            showTime: true
        },
        {
            title: "更新时间",
            dataIndex: "updated_at",
            formType: "date",
            addDisplay: false,
            editDisplay: false,
            hide: true,
            showTime: true
        },
        {
            title: "删除时间",
            dataIndex: "deleted_at",
            formType: "date",
            addDisplay: false,
            editDisplay: false,
            hide: true,
            showTime: true
        },
        {
            title: "备注",
            dataIndex: "remark",
            formType: "textarea",
            hide: true
        }
    ])
}